from flask_wtf import FlaskForm
from wtforms import (StringField, RadioField, SelectField, TextAreaField,
                     SelectMultipleField, PasswordField, BooleanField, FileField, URLField)
from wtforms.validators import DataRequired, Length, URL, Email
from app.blog.models import PostPublishType
from flask_wtf.file import FileField, FileRequired, FileSize, FileAllowed


class CategoryCreateForm(FlaskForm):
    # 分类表单
    name = StringField('分类名称', validators=[
        DataRequired(message="不能为空"),
        Length(max=128, message="不符合字数要求！")
    ])
    icon = StringField('分类图标', validators=[
        Length(max=256, message="不符合字数要求！")
    ])


class PostForm(FlaskForm):
    # 添加文章表单
    title = StringField('标题', validators=[
        DataRequired(message="不能为空"),
        Length(max=128, message="不符合字数要求！")
    ])
    desc = StringField('描述', validators=[
        DataRequired(message="不能为空"),
        Length(max=200, message="不符合字数要求！")
    ])
    has_type = RadioField('发布状态',
                          choices=(PostPublishType.draft.name, PostPublishType.show.name),
                          default=PostPublishType.show.name)
    category_id = SelectField(
        '分类',
        choices=None,
        coerce=int,
        validators=[
            DataRequired(message="不能为空"),
        ]
    )
    content = TextAreaField('文章详情',
                            validators=[DataRequired(message="不能为空")]
                            )


class Friend_PostForm(FlaskForm):
    # 添加文章表单
    title = StringField('标题', validators=[
        DataRequired(message="不能为空"),
        Length(max=128, message="不符合字数要求！")
    ])
    desc = StringField('描述', validators=[
        DataRequired(message="不能为空"),
        Length(max=200, message="不符合字数要求！")
    ])
    has_type = RadioField('发布状态',
                          choices=(PostPublishType.draft.name, PostPublishType.show.name),
                          default=PostPublishType.show.name)
    category_id = SelectField(
        '分类',
        choices=None,
        coerce=int,
        validators=[
            DataRequired(message="不能为空"),
        ]
    )
    new_old = SelectField(
        '分类',
        choices=None,
        coerce=int,
        validators=[
            DataRequired(message="不能为空"),
        ]
    )
    content = TextAreaField('文章详情',
                            validators=[DataRequired(message="不能为空")]
                            )


class CreateUserForm(FlaskForm):
    # 创建用户表单
    username = StringField('username', validators=[
        DataRequired(message="Not null"),
        Length(max=32, message="不符合字数要求！")
    ])
    email = StringField('email', validators=[
        DataRequired(message="Not null"),
        Length(min=6, max=64, message="超过限制字数！"),  # 调整长度限制以适应实际的邮箱地址长度
        Email(message="邮箱格式不正确，请输入有效的邮箱地址")  # 添加邮箱格式验证
    ])
    password = PasswordField('password', validators=[
        # DataRequired(message="不能为空"),
        Length(max=32, message="不符合字数要求！")
        # ], description="修改用户信息时，留空则代表不修改！")
    ], description="Empty represents no modification")
    avatar = FileField("avatar", validators=[
        FileAllowed(['jpg', 'png', 'gif'], message="仅支持jpg/png/gif格式"),
        FileSize(max_size=2048000, message="No more than 2M")],
                       # description="大小不超过2M，仅支持jpg/png/gif格式，不选择则代表不修改")
                       description="No more than 2M，only jpg/png/gif，Not selecting means not modifying")
    is_super_user = BooleanField("Set as Coordinator")
    is_active = BooleanField("Active", default=True)
    is_staff = BooleanField("Lock")
